//SYSC3303-assignment#1
//Student Name: Qi Zhang
//Student No. 100839438
//Date:12,July,2011

import java.io.*;
import java.net.*;

public class Intermediate {

   DatagramPacket sendPacket1, receivePacket1;
   DatagramPacket sendPacket2, receivePacket2;
   DatagramSocket sendSocket1, receiveSocket;
   DatagramSocket sendSocket2;
   
   public Intermediate()
   {
      try {
         // Construct a datagram socket and bind it to 68 
         // port on the local host machine. This socket will be used to
         // receive UDP Datagram packets.
         receiveSocket = new DatagramSocket(68); //receive the request from client and server
         sendSocket1= new DatagramSocket(); //send packet to client 
         sendSocket2= new DatagramSocket(); //send packet to server    
       } catch (SocketException e) {   // Can't create the socket.
         e.printStackTrace();
         System.exit(1);
      }
   }

   public void sendAndReceive()
   {
    byte [] data1=new byte[17];  //request
    byte [] data2=new byte[4];   //response  
    receivePacket1=new DatagramPacket(data1,data1.length); //packet send to server
    receivePacket2=new DatagramPacket(data2,data2.length); //packet send to client
    
    while(1<3){
    //**waiting for the request from client**//
    try {
        receiveSocket.receive(receivePacket1);
     } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
     }

    System.out.println("intermediate: Received the request.");
    data1=receivePacket1.getData();
    System.out.println("Intermediate: Containing: (Bytes)"+data1); 
    String s1=new String(data1);
    System.out.println("Intermediate: Containing:(String)"+ s1);

//Send the packet2 to server via the sendsocket2
      try {
         sendPacket1 = new DatagramPacket(data1,receivePacket1.getLength(),
                                         InetAddress.getLocalHost(),69);
      } catch (UnknownHostException e) {
         e.printStackTrace();
         System.exit(1);
      }
      
      try {
          sendSocket2.send(sendPacket1);
        } catch (IOException e) {  
           e.printStackTrace();
           System.exit(1);
        }

      System.out.println("Intermediate: request sent");

//**waiting for the response from server**//
      try {
          receiveSocket.receive(receivePacket2);
       } catch (IOException e) {
          e.printStackTrace();
          System.exit(1);
       }

      System.out.println("Intermediate:response received.");
      data2 = receivePacket2.getData(); 
      String s2=new String(data2);
      System.out.println("Intermediate: Containing:(String)"+ s2);

      try {
          sendPacket2 = new DatagramPacket(data2,receivePacket2.getLength(),
                                     InetAddress.getLocalHost(), 5000);
            } catch (UnknownHostException e) {
               e.printStackTrace();
               System.exit(1);
            }
      try {
          sendSocket1.send(sendPacket2);
        } catch (IOException e) {  
           e.printStackTrace();
           System.exit(1);
        }
  
      System.out.println("Intermediate: response sent");
      System.out.println("-----------------------------");
    }
      }
      
   public static void main(String args[])
   {
      Intermediate c = new Intermediate();
      c.sendAndReceive();
   }
}